package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.bean.WechatSendRepair;
import com.jy.adscm.wechat.mapper.mysql.WechatSendRepairMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.WechatRepairService;
import com.jy.adscm.wechat.service.WechatSendRepairService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-26
 */
@Service
public class WechatSendRepairServiceImpl extends ServiceImpl<WechatSendRepairMapper, WechatSendRepair> implements WechatSendRepairService {
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    WechatRepairService wechatRepairService;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public WechatSendRepair insertWechatSendRepair(WechatSendRepair wechatSendRepair) {
        //查找最大的自修单号
        synchronized (this){
            String orderNums="";
            String date= new SimpleDateFormat("yyMMdd").format(new Date());
            Wrapper<WechatSendRepair> wrapper =new EntityWrapper<>();
            wrapper.like("order_nums",date, SqlLike.RIGHT);
            wrapper.orderBy("order_nums",false);
            wrapper.last("limit 1");
            List<WechatSendRepair> wechatSendRepairs = super.selectList(wrapper);
            if (wechatSendRepairs!=null && wechatSendRepairs.size()>0){
                orderNums= String.valueOf(Long.valueOf(wechatSendRepairs.get(0).getOrderNums())+1) ;

            }else {
                orderNums=date+"0001";
            }
            wechatSendRepair.setOrderNums(orderNums);
            wechatSendRepair.setState(0);
            super.insert(wechatSendRepair);
            return wechatSendRepair;
        }
    }

    @Override
    public Page<WechatSendRepair> selectWechatSendRepair(Page<WechatSendRepair> page, QueryWechatSendRepair queryWechatSendRepair) throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatSendRepair.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return page;
        }
        //查询是否是浆站人员
        Wrapper<WechatSendRepair> wrapper=new EntityWrapper<>();
        wrapper.eq("repairer",wechatUser.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryWechatSendRepair.getState(),null)){
            wrapper.eq("state",queryWechatSendRepair.getState());
        }
        page=super.selectPage(page,wrapper);
        List<WechatSendRepair> wechatSendRepairs = page.getRecords();
        for (WechatSendRepair wechatSendRepair : wechatSendRepairs) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSendRepair.getPid(),null)){
                WechatRepair wechatRepair = wechatRepairService.selectWechatRepairById(wechatSendRepair.getPid());
                wechatSendRepair.setWechatRepair(wechatRepair);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSendRepair.getRepairer(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatSendRepair.getRepairer());
                wechatSendRepair.setRepairerName(wechatUser1.getName());
            }
        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public boolean updateWechatSendRepair(WechatSendRepair wechatSendRepair) {
        WechatSendRepair oldWechatSendRepair = super.selectById(wechatSendRepair.getId());
        //已经处理，有了处理结果了
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSendRepair.getResult(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(oldWechatSendRepair.getResult(),null)){
                wechatSendRepair.setState(1);
            }
        }
        boolean b = super.updateById(wechatSendRepair);
        return b;
    }

    @Override
    public WechatSendRepair selectWechatSendRepairById(Long id) {
        WechatSendRepair wechatSendRepair = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSendRepair.getPid(),null)){
            WechatRepair wechatRepair = wechatRepairService.selectWechatRepairById(wechatSendRepair.getPid());
            wechatSendRepair.setWechatRepair(wechatRepair);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatSendRepair.getRepairer(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatSendRepair.getRepairer());
            wechatSendRepair.setRepairerName(wechatUser1.getName());
        }
        return wechatSendRepair;
    }

    @Override
    public WechatSendRepair selectByPid(Long id) {
        Wrapper<WechatSendRepair> wrapper=new EntityWrapper<>();
        wrapper.eq("pid",id);
        List<WechatSendRepair> WechatSendRepairs = super.selectList(wrapper);

        if (WechatSendRepairs!=null && WechatSendRepairs.size()>0){
            WechatSendRepair wechatSendRepair = WechatSendRepairs.get(0);
            return wechatSendRepair;
        }
        return null;
    }
}
